package cc.linker.steplink.crm.merchant.service;

import cc.linker.steplink.crm.commons.coupon.CouponCustomerInfo;
import cc.linker.steplink.crm.commons.service.CommonsService;
import cc.linker.steplink.crm.commons.utils.NumberUtils;
import cc.linker.steplink.crm.merchant.dao.VerificateToolDao;
import cc.linker.steplink.crm.merchant.pojo.VerificatePo;
import cc.linker.steplink.crm.merchant.pojo.VerificaterPo;
import cc.linker.steplink.crm.merchanth5.service.coupon.CouponH5Service;
import cc.linker.steplink.crm.response.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Service
public class VerificateToolService {

    @Autowired
    private VerificateToolDao verificateToolDao;
    @Autowired
    private CouponH5Service couponH5Service;
    @Autowired
    private CommonsService commonsService;


    public List<VerificatePo> getList(Map<String, Object> params) {
        return verificateToolDao.getList(params);
    }

    public void recovery(Map<String, Object> params) {
        if (StringUtils.isEmpty(params.get("id"))) {
            throw GlobalException.build("参数错误！");
        }
        VerificatePo verificatePo = verificateToolDao.getInfo(params);
        Integer couponSource = verificatePo.getCouponSource();
        if (couponSource == 2) {
            throw GlobalException.build("外部来源不可恢复！");
        } else {
            //调用中台取消使用
            String consumptionId = verificatePo.getVerificateId();
            String couponId = verificatePo.getCouponId();
            if (!StringUtils.isEmpty(consumptionId) && !StringUtils.isEmpty(couponId)) {
                String orgId = verificatePo.getOrgId();
                String customerId = verificatePo.getCustomerId();
                CouponCustomerInfo couponCustomerInfo = CouponCustomerInfo
                        .builder()
                        .customerId(customerId)
                        .customerName("")
                        .customerPhone("")
                        .cover("")
                        .orgId(orgId)
                        .build();
//                        CouponCustomerInfo couponCustomerInfo = (CouponCustomerInfo) params.get("couponCustomerInfo");
                //使用优惠券回滚
                couponH5Service.useConsumption(consumptionId, null, null, null, null, 1, couponCustomerInfo);
                verificateToolDao.recovery(params);
            }
        }
    }


    public  List<VerificaterPo>  getVerificaterlist(Map<String, Object> params) {
        return verificateToolDao.getVerificaterlist(params);
    }

    public void deleteVerificater(Map<String, Object> params) {
        verificateToolDao.deleteVerificater(params);
    }

    public void passVerificater(Map<String, Object> params) {
        verificateToolDao.passVerificater(params);
    }

    public int getVerificateCount(Map<String, Object> params) {
        return verificateToolDao.getVerificateCount(params);
    }

    public void exportlist(Map<String, Object> params, HttpServletResponse response) throws Exception{
        // 卡券名称 核销号 卡券类型 渠道 卡券内容 用户手机 核销时间 核销员
        String[] title = {"卡券名称", "核销号", "卡券类型", "渠道", "卡券内容", "用户手机", "核销时间", "核销员"};
        String fileName = "";
        if(StringUtils.isEmpty(params.get("startTime")) || StringUtils.isEmpty(params.get("endTime"))){
            fileName = "核销记录";
        }else{
            fileName = "核销记录(" + params.get("startTime").toString()+ "-" + params.get("endTime").toString()+ ")";
        }
        String[] strings = {"couponName", "verificateId", "couponType", "couponSource", "couponContent","customerPhone", "verificateTime", "verificateUserName"};
        List<Map<String, Object>> list =  verificateToolDao.getMapList(params);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).put("verificateTime",list.get(i).get("verificateTime").toString().substring(0,19));
            Integer couponType =  (Integer) list.get(i).get("couponType");
            String couponTypeString = "";
            if(couponType!=null){
                if(couponType==0){
                    couponTypeString = "满减券";
                }else if(couponType==1){
                    couponTypeString = "折扣券";
                }else if(couponType==2){
                    couponTypeString = "抵扣券";
                }else if(couponType==3){
                    couponTypeString = "礼品券";
                }
            }
            list.get(i).put("couponType",couponTypeString);
            Integer couponSource =  (Integer) list.get(i).get("couponSource");
            String couponSourceString = "";
            if(couponSource!=null){
                if(couponSource==1){
                    couponSourceString = "内部";
                }else if(couponSource==2){
                    couponSourceString = "外部";
                }
            }
            list.get(i).put("couponSource",couponSourceString);
        }
        commonsService.excelDownload(list, strings, title, fileName, response);
    }
}
